﻿

var templateLib = [
                 { 'name' : 'dropdown_menu', 'source' : 'views-dropdown.htm' }, 
                 { 'name' : 'invoice_event_row', 'source' : 'views-invoice.event.row.htm' },
                 { 'name' : 'invoice_property_row', 'source' : 'views-invoice.property.row.htm' },
                 { 'name' : 'toolBar', 'source' : 'views-global.toolbar.htm' },
                 { 'name' : 'dateSelect', 'source' : 'views-fancy.text.htm' },
                 { 'name' : 'codewindow', 'source' : 'views-dialog.htm' },
                 { 'name' : 'messagebox', 'source' : 'views-alert.htm' }
                ];
                
window.templates = {};    
var Factory = window.templates;
Factory.copy = function (o) { for (var i in o) this[i]=o[i]; } 
Factory.draw = function () { return Factory.write (this); }
Factory.empty = function (skin) { return { 'template' : skin, 'draw' : Factory.draw, 'copy' : Factory.copy }; };
Factory.create = function () { var x={}, o=arguments; for (var i=0;i<o.length;i+=2) x[o[i]]=o[i+1]; return x.template ? Factory.write (x) : x;  };
Factory.write = function (o) { if (o.template && this[o.template]) return this[o.template].parse (o); } 
                
function NamedStringBuilder (name, text)
{
    this.name 		= name;
    this.text 		= text;
	this.fields 	= {};
	
	this.load = function ()
	{
        var str, ok, rx = [ /\((\w+)\)/g, /<cn\:(\w+)\s*\/>/g ];
		for (var e in rx)
		{
			str = this.text;
			ok  = rx[e].exec (str);
			while (ok)
			{
				if (!this.fields[ok[1]] && isNaN(ok[1])) this.fields[ok[1]]=ok[1]; 
				str = str.substr (rx[e].lastIndex);
				ok  = rx[e].exec (str)
			}
		}
	}
    this.insert = function (text, name, value)
    { 
		while (text.indexOf ('<cn:' + name + ' />') > 0) text = text.replace ('<cn:' + name + ' />', value);
		while (text.indexOf ('(' + name + ')') > 0) text = text.replace ('(' + name + ')', value);
		return text;
	}
    this.parse = function (object)
    { 
        var msg, htm = this.text;  
		for (var e in this.fields)
		{
		    if (object[e]) { }
		    else if (object[e]=='') { }
			else 
			{
				msg='<img src="/webservices/images/explorer/alerticon.jpg" title="" align="absmiddle">' + 
				     ' The value for "' + e + '" was not provided in template <b>' + this.name + ' </b>!';
				htm = this.insert(htm, e, msg);  
			}
		}
        for (var e in object) htm = this.insert (htm, e, object[e]);  
        return htm;
    }  
	this.load ();
}

function registerTemplate (name, text)
{ 
    if (!Factory [ name ]) Factory [ name ] = new NamedStringBuilder( name, text );   
} 



function downloadTemplates ()
{
    if (templateLib.length > 0)
    {
        window.template = templateLib.pop (); 
        var Connector = { 
                    'params' : { 'view' : window.template.source },  
                    'message' : 'downloading template ' + window.template.name,
                    'aftersend' : onTemplateDownloaded
                  };
        window.request.create (Connector);   
    } 
    else 
    { 
//        if (!handleQueryString (location.href, null, true))
            LoginScreenEvent.invoke ();
    }
}

function onTemplateDownloaded ()
{
    registerTemplate (window.template.name, window.http.responseText); 
    downloadTemplates ();
}

